<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="客户来源" prop="clientSource">
                      <el-select v-model="formData.clientSource" placeholder="请选择客户来源">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.HOUSE_CLIENT_SOURCE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="意向区域" prop="intendedArea">
                      <el-input v-model="formData.intendedArea" placeholder="请输入意向区域" />
                    </el-form-item>
                    <el-form-item label="客户状态" prop="status">
                      <el-select v-model="formData.status" placeholder="请选择客户状态">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.HOUSE_CLIENT_STATUS)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="房源方" prop="houseType">
                      <el-select v-model="formData.houseType" placeholder="请选择房源方">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CLIENT_HOUSE_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="客户跟进人" prop="chargePerson">
<!--                      <el-input v-model="formData.chargePerson" placeholder="请输入客户跟进人" />-->
                      <el-select v-model="formData.chargePerson" placeholder="跟进人">
                        <el-option v-for="user in users" :key="parseInt(user.id)" :label="user.nickname" :value="parseInt(user.id)"/>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="预算" prop="budget">
                      <el-input v-model="formData.budget" placeholder="请输入预算" />
                    </el-form-item>
                    <el-form-item label="租房时间" prop="clientTime">
                      <el-date-picker clearable v-model="formData.clientTime" type="date" value-format="timestamp" placeholder="选择租房时间" />
                    </el-form-item>
                    <el-form-item label="对接时间" prop="integrationTime">
                      <el-date-picker clearable v-model="formData.integrationTime" type="date" value-format="timestamp" placeholder="选择对接时间" />
                    </el-form-item>
                    <el-form-item label="微信号" prop="wechatNo">
                      <el-input v-model="formData.wechatNo" placeholder="请输入微信号" />
                    </el-form-item>
                    <el-form-item label="手机号" prop="mobile">
                      <el-input v-model="formData.mobile" placeholder="请输入手机号" />
                    </el-form-item>
                    <el-form-item label="是否结算" prop="settled">
                      <el-select v-model="formData.settled" placeholder="请选择是否结算">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="结算金额" prop="amount">
                      <el-input v-model="formData.amount" placeholder="请输入结算金额" />
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                      <el-input v-model="formData.remark" placeholder="请输入备注" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as HouseClientInfoApi from '@/api/system/houseclientinfo';
  import {listSimpleUsers} from "@/api/system/user";

  export default {
    name: "HouseClientInfoForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,

        users: [],
        // 表单参数
        formData: {
                            id: undefined,
                            clientSource: undefined,
                            intendedArea: undefined,
                            status: undefined,
                            houseType: undefined,
                            chargePerson: undefined,
                            budget: undefined,
                            clientTime: undefined,
                            integrationTime: undefined,
                            wechatNo: undefined,
                            mobile: undefined,
                            settled: undefined,
                            amount: undefined,
                            remark: undefined,
        },
        // 表单校验
        formRules: {
                        clientSource: [{ required: true, message: '客户来源不能为空', trigger: 'change' }],
                        intendedArea: [{ required: true, message: '意向区域不能为空', trigger: 'blur' }],
                        status: [{ required: true, message: '客户状态不能为空', trigger: 'change' }],
                        houseType: [{ required: true, message: '房源方不能为空', trigger: 'change' }],
                        chargePerson: [{ required: true, message: '客户跟进人不能为空', trigger: 'blur' }],
                        budget: [{ required: true, message: '预算不能为空', trigger: 'blur' }],
                        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
                        settled: [{ required: true, message: '是否结算不能为空', trigger: 'change' }],
                        amount: [{ required: true, message: '结算金额不能为空', trigger: 'blur' }],
        },
                        };
    },

    created() {
      // 获得用户列表
      listSimpleUsers().then(response => {
        this.users = response.data;
      })
    },

    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await HouseClientInfoApi.getHouseClientInfo(id);
            this.formData = res.data;
            this.title = "修改客户管理信息";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增客户管理信息";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await HouseClientInfoApi.updateHouseClientInfo(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await HouseClientInfoApi.createHouseClientInfo(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            clientSource: undefined,
                            intendedArea: undefined,
                            status: undefined,
                            houseType: undefined,
                            chargePerson: undefined,
                            budget: undefined,
                            clientTime: undefined,
                            integrationTime: undefined,
                            wechatNo: undefined,
                            mobile: undefined,
                            settled: undefined,
                            amount: undefined,
                            remark: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
